unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, jpeg, RzBckgnd, StdCtrls, RzStatus, Ctrl, Math,
  ActnList, StrUtils;

type
  Tmainfrm = class(TForm)
    HeadImage: TImage;
    BottomImage: TImage;
    LeftImage: TImage;
    RightImage: TImage;
    PalCtrl: TPanel;
    Image1: TImage;
    pal_menu: TPanel;
    Image2: TImage;
    labClose: TLabel;
    pal_frmparent: TPanel;
    MarqueeStatus: TRzMarqueeStatus;
    RzClockStatus: TRzClockStatus;
    Image3: TImage;
    labMin: TLabel;
    Background: TRzBackground;
    labCompanyName: TLabel;
    Timer: TTimer;
    Label1: TLabel;
    Image22: TImage;
    CtrlFrame: TCtrlFrame;
    labsftname: TLabel;
    syscmd: TActionList;
    actclose: TAction;
    actMin: TAction;
    actMax: TAction;
    SaveDialog1: TSaveDialog;
    procedure FormResize(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure labCloseMouseEnter(Sender: TObject);
    procedure labCloseMouseLeave(Sender: TObject);
    procedure pal_frmparentResize(Sender: TObject);
    procedure labMinMouseEnter(Sender: TObject);
    procedure labMinMouseLeave(Sender: TObject);
    procedure labMinClick(Sender: TObject);
    procedure labCloseClick(Sender: TObject);
    procedure CtrlFrame1labmenu_logoutClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure CtrlFramelabmenu_logoutClick(Sender: TObject);
    procedure CtrlFramelabmenu_employeeClick(Sender: TObject);
    procedure CtrlFramelabmenu_clientClick(Sender: TObject);
    procedure CtrlFrameLabel3Click(Sender: TObject);
    procedure CtrlFramelabmenu_productionClick(Sender: TObject);
    procedure CtrlFramelabmenu_wtloggerClick(Sender: TObject);
    procedure CtrlFrameLabel1Click(Sender: TObject);
    procedure CtrlFrameLabel2Click(Sender: TObject);
    procedure CtrlFramelabmenu_syssetClick(Sender: TObject);
    procedure CtrlFramelabmenu_clntrptClick(Sender: TObject);
    procedure actcloseExecute(Sender: TObject);
    procedure actMinExecute(Sender: TObject);
    procedure actMaxExecute(Sender: TObject);
    procedure CtrlFramelabmenu_rlvideoClick(Sender: TObject);
    procedure CtrlFramelabmenu_picmanageClick(Sender: TObject);
    procedure CtrlFramelabmenu_cmprsrprClick(Sender: TObject);
    procedure CtrlFramelabmenu_usermanageClick(Sender: TObject);
    procedure CtrlFramelabmenu_abutClick(Sender: TObject);
    procedure CtrlFramelabmenu_helpClick(Sender: TObject);
    procedure CtrlFramelabmenu_barGrapClick(Sender: TObject);
  published
    { published declarations }
  private
    { Private declarations }
  public
    { Public declarations }
    procedure setInfo(values:array of string);
    //procedure setScrollBar(value:string);
    procedure app_close;
    procedure hideChildForm;
    function pwleg(Index:integer):boolean;
  end;

var
  mainfrm: Tmainfrm;

implementation

uses login, includefile, IniFiles, picker, product, maindbview, rfidctrl, summary,
     payslip, productrpt, sysstting, rcdLog, dbmodule, dqc, mqc, usrmng,
  bargraph;

{$R *.dfm}

const
  HeadImageHeight=75;
  BottomImageHeight=105;
  LeftImageWidth=31;
  RightImageWidth=20;
  OWidth=1024;
  OHeight=768;
  PalCtrlWidth=174;
  PalCtrlHeight=584;
  CloseLeft=961;
  CloseTop=14;
  CloseWidth=50;
  MinLeft=911;
  MinWidth=26;
  ClockLeft=858;

function Tmainfrm.pwleg(Index:integer):boolean;
begin
  Result:=False;
  userpwr:=Trim(userpwr);
  if userpwr[Index]='Y' then
    Result:=True;
end;

procedure Tmainfrm.hideChildForm;
begin
  pickerfrm.Hide;
  productfrm.Hide;
  maindbviewfrm.Hide;
  rfidctrlfrm.Hide;
  summaryfrm.Hide;
  payslipfrm.Hide;
  productrptfrm.Hide;
  syssttingfrm.Hide;
  rcdlogfrm.Hide;
  dqcfrm.Hide;
  mqcfrm.Hide;
  frmBarGraph.hide;
end;

procedure Tmainfrm.app_close;
begin
  rcdlogfrm.close;
  if bCanClose then begin
    pickerfrm.close;
    productfrm.close;
    maindbviewfrm.close;
    rfidctrlfrm.close;
    summaryfrm.close;
    payslipfrm.close;
    productrptfrm.close;
    syssttingfrm.close;
    mainfrm.close;
  end;
end;

procedure Tmainfrm.SetInfo(values:array of string);
begin
  labcompanyname.Caption:=values[0];
  MarqueeStatus.Caption:=values[1];
  labsftname.Caption:=values[2];
  with CtrlFrame do begin
    labcmpyname.Caption:=values[0];
    labaddr1.Caption:=values[3];
    labaddr2.Caption:=values[4];
    labaddr3.Caption:=values[5];
    labfax.Caption:=values[6];
    labtel.Caption:=values[7];
    laburl.Caption:=values[8];
  end;
end;

procedure Tmainfrm.FormResize(Sender: TObject);
begin
  leftImage.Width:=Round(LeftImageWidth*ClientWidth/OWidth);
  RightImage.Width:=Round(RightImageWidth*ClientWidth/OWidth);
  Image2.Top:=CloseTop;
  Image2.Left:=Round(CloseLeft*ClientWidth/OWidth);
  Image2.Width:=Round(CloseWidth*ClientWidth/OWidth);
  
  Image3.Top:=CloseTop;
  Image3.Left:=Round(MinLeft*ClientWidth/OWidth);
  Image3.Width:=Round(MinWidth*ClientWidth/OWidth);

  labClose.Top:=CloseTop;
  labClose.Left:=Image2.Left;
  labclose.Width:=Image2.Width;

  labMin.Top:=CloseTop;
  labMin.Left:=Image3.Left;
  labMin.Width:=Image3.Width; 

  MarqueeStatus.Left:=15;
  MarqueeStatus.Width:=ClientWidth-30;
  MarqueeStatus.Top:=ClientHeight-MarqueeStatus.Height-3;
  RzClockStatus.Width:=Round(170*ClientWidth/OWidth);
  RzClockStatus.Left:=ClientWidth-RzClockStatus.Width-3;
  RzClockStatus.Top:= ClientHeight-80;
  left:=0;
  top:=0;
end;

procedure Tmainfrm.Image1Click(Sender: TObject);
begin
  if palCtrl.Width >9 then begin
    while palCtrl.Width>9 do
      palCtrl.Width:=palCtrl.Width-2;
  end else begin
    while palCtrl.Width<174 do
      palCtrl.Width:=palCtrl.Width+2;
  end;
  palCtrl.Left:=rightimage.Left-palCtrl.Width;
end;

procedure Tmainfrm.labCloseMouseEnter(Sender: TObject);
begin
  Image2.Show;
end;

procedure Tmainfrm.labCloseMouseLeave(Sender: TObject);
begin
  Image2.Hide; 
end;

procedure Tmainfrm.pal_frmparentResize(Sender: TObject);
begin
  labCompanyName.Width:=pal_frmparent.ClientWidth;
  labsftname.Width:=pal_frmparent.ClientWidth;
  labCompanyName.Top:=Round(136*pal_frmparent.ClientHeight/PalCtrlHeight);
  labsftname.Top:=Round(256*pal_frmparent.ClientHeight/PalCtrlHeight);
end;

procedure Tmainfrm.labMinMouseEnter(Sender: TObject);
begin
  Image3.Show;
end;

procedure Tmainfrm.labMinMouseLeave(Sender: TObject);
begin
  Image3.Hide;
end;

procedure Tmainfrm.labMinClick(Sender: TObject);
begin
  PostMessage(Handle,WM_SYSCOMMAND,SC_ICON,0);
end;

procedure Tmainfrm.labCloseClick(Sender: TObject);
begin
  app_close;
end;

procedure Tmainfrm.CtrlFrame1labmenu_logoutClick(Sender: TObject);
begin
  with Tloginfrm.Create(Application) do begin
    case showmodal of
      mrok: free;
      mrcancel: mainfrm.close;
    end;
  end;
end;

procedure Tmainfrm.FormShow(Sender: TObject);
begin
  with Tloginfrm.Create(Application) do begin
    case showmodal of
      mrok: begin
        free;
        mainfrm.show;
      end;
      mrcancel: mainfrm.close;
    end;
  end;
  Timer.Enabled:=True;
end;

procedure Tmainfrm.FormCreate(Sender: TObject);
var
  I:integer;
begin
  CtrlFrame.IniLanguage;
  for I:=0 to ComponentCount-1 do begin
    if Components[I] is TLabel then begin
      if 0<>TLabel(Components[I]).Tag then
        TLabel(Components[I]).Caption:=
                GetCaption(name,TLabel(Components[I]).Tag);
    end;
  end;
  RzClockStatus.Format:=DateFrtStr+' hh:mm:ss';
  with inif do begin
    SetInfo([
             ReadString('system','companyname',''),
             ReadString('system','marquee',''),
             ReadString('system','software',''),
             ReadString('system','ExtendInfo1',''),
             ReadString('system','ExtendInfo2',''),
             ReadString('system','ExtendInfo3',''),
             ReadString('system','ExtendInfo4',''),
             ReadString('system','ExtendInfo5',''),
             ReadString('system','ExtendInfo6','')
            ]);
  end;
  Application.HelpFile:=GetAppPath+'\berry.hlp';
end;

procedure Tmainfrm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  inif.WriteInteger('system','User',usrIndex);
  inif.WriteInteger('system','SMRRpt',SMRRpt);
  lngf.Free;
  inif.Free;
end;

procedure Tmainfrm.CtrlFramelabmenu_logoutClick(Sender: TObject);
begin
  if not pwleg(TLabel(Sender).Tag-50000) then exit;
  hideChildForm;
  with Tloginfrm.Create(Application) do begin
    case showmodal of
      mrok: free;
      mrcancel: mainfrm.close;
    end;
  end;
end;

procedure Tmainfrm.CtrlFramelabmenu_employeeClick(Sender: TObject);
begin
  if not pwleg(TLabel(Sender).Tag-50000) then exit;
  hideChildForm;
  pickerfrm.Parent:=pal_frmparent;
  pickerfrm.Show;
  pickerfrm.IniParam;
end;

procedure Tmainfrm.CtrlFramelabmenu_clientClick(Sender: TObject);
begin
  if not pwleg(TLabel(Sender).Tag-50000) then exit;
  hideChildForm;
  productfrm.parent:=pal_frmparent;
  productfrm.Show;
  productfrm.IniParam;
end;

procedure Tmainfrm.CtrlFrameLabel3Click(Sender: TObject);
begin
  if not pwleg(TLabel(Sender).Tag-50000) then exit;
  hideChildForm;
  maindbviewfrm.parent:=pal_frmparent;
  maindbviewfrm.Show;
  maindbviewfrm.IniParam;
end;

procedure Tmainfrm.CtrlFramelabmenu_productionClick(Sender: TObject);
begin
  if not pwleg(TLabel(Sender).Tag-50000) then exit;
  hideChildForm;
  rfidctrlfrm.ShowModal;
end;

procedure Tmainfrm.CtrlFramelabmenu_wtloggerClick(Sender: TObject);
begin
  if not pwleg(TLabel(Sender).Tag-50000) then exit;
  hideChildForm;
  summaryfrm.Parent:=pal_frmparent;
  summaryfrm.refreshDB;
  summaryfrm.Show;
end;

procedure Tmainfrm.CtrlFrameLabel1Click(Sender: TObject);
begin
  if not pwleg(TLabel(Sender).Tag-50000) then exit;
  hideChildForm;
  payslipfrm.Parent:=pal_frmparent;
  payslipfrm.Show;
end;

procedure Tmainfrm.CtrlFrameLabel2Click(Sender: TObject);
begin
  if not pwleg(TLabel(Sender).Tag-50000) then exit;
  hideChildForm;
  productrptfrm.Parent:=pal_frmparent;
  productrptfrm.Show;
end;

procedure Tmainfrm.CtrlFramelabmenu_syssetClick(Sender: TObject);
begin
  if not pwleg(TLabel(Sender).Tag-50000) then exit;
  hideChildForm;
  syssttingfrm.Parent:=pal_frmparent;
  syssttingfrm.Show;
  //syssttingfrm.drawQcfrm1.getChatData('00002',str2Date('09-06-2009'));
end;

procedure Tmainfrm.CtrlFramelabmenu_clntrptClick(Sender: TObject);
begin
  if not pwleg(TLabel(Sender).Tag-50000) then exit;
  hideChildForm;
  rcdLogfrm.Parent:=pal_frmparent;
  rcdLogfrm.Show;
end;

procedure Tmainfrm.actcloseExecute(Sender: TObject);
begin
  app_close;
end;

procedure Tmainfrm.actMinExecute(Sender: TObject);
begin
  PostMessage(Handle,WM_SYSCOMMAND,SC_ICON,0);
end;

procedure Tmainfrm.actMaxExecute(Sender: TObject);
begin
  PostMessage(Handle,WM_SYSCOMMAND,SC_ZOOM,0);
end;

procedure Tmainfrm.CtrlFramelabmenu_rlvideoClick(Sender: TObject);
begin
  if not pwleg(TLabel(Sender).Tag-50000) then exit;
  hideChildForm;
  dqcfrm.Parent:=pal_frmparent;
  dqcfrm.Show;
end;

procedure Tmainfrm.CtrlFramelabmenu_picmanageClick(Sender: TObject);
begin
  if not pwleg(TLabel(Sender).Tag-50000) then exit;
  hideChildForm;
  mqcfrm.Parent:=pal_frmparent;
  mqcfrm.Show;
end;

procedure Tmainfrm.CtrlFramelabmenu_cmprsrprClick(Sender: TObject);
var
  tmpf,psswd,destfp,exfn:string;
begin
  if not pwleg(TLabel(Sender).Tag-50000) then exit;
  hideChildForm;
  tmpf:=inif.ReadString('system','dbsource','');
  tmpf:=Trim(tmpf);
  exfn:=RightStr(tmpf,Length(tmpf)+1-LastDelimiter('.',tmpf));
  psswd:=inif.ReadString('system','dbpassword','harvest');
  SaveDialog1.Execute;
  destfp:=SaveDialog1.FileName;

  if destfp='' then exit;
  
  if pos(exfn,destfp)<=0 then
    destfp:=destfp+exfn;
    
  if not copyfile(pchar(tmpf),Pchar(destfp),false) then begin

  end else begin
    If IDYES=MessageBox(handle,
                        'No = Dackup main data file'+#13#10+#13#10+
                        'Yes= Delete main data file after'+#13#10+
                        'backup is done',
                        'MAIN DATABASE BACKUP UTILITY',
                        MB_YESNO+MB_ICONINFORMATION) then begin
      with dbmodulefrm.dbtmpqry do begin
        active :=False;
        sql.Clear;
        sql.Add('Delete from Record');
        execsql;
      end;
    end;
    if not CompactDatabase(tmpf,psswd) then exit;
  end;
end;

procedure Tmainfrm.CtrlFramelabmenu_usermanageClick(Sender: TObject);
begin
  if not pwleg(TLabel(Sender).Tag-50000) then exit;
  hideChildForm;
  usrmngfrm.refreshdb;
  usrmngfrm.showmodal;
end;

procedure Tmainfrm.CtrlFramelabmenu_abutClick(Sender: TObject);
begin
  //if not pwleg(TLabel(Sender).Tag-50000) then exit;
  //
  //Application.HelpFile:=GetAppPath+'\berry.hlp';
  Application.HelpCommand(Help_Contents,0);
end;

procedure Tmainfrm.CtrlFramelabmenu_helpClick(Sender: TObject);
begin
  //if not pwleg(TLabel(Sender).Tag-50000) then exit;
  //Application.HelpFile:=GetAppPath+'\berry.hlp';
  Application.HelpCommand(Help_Contents,0);
end;

procedure Tmainfrm.CtrlFramelabmenu_barGrapClick(Sender: TObject);
begin
  hideChildForm;
  frmBarGraph.Parent:=pal_frmparent;
  frmBarGraph.Show;
end;

end.
